import '@babel/polyfill';
import * as THREE from 'three';
import { OrbitControls } from 'three/examples/jsm/controls/OrbitControls';
import gsap from 'gsap';
import * as dat from 'dat.gui';

//星系

//场景
const scene = new THREE.Scene();

//相机
const camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 200);
camera.position.set(0, 0, 20);
scene.add(camera);

//渲染器
const renderer = new THREE.WebGL1Renderer({ antialias: true });
renderer.setSize(window.innerWidth, window.innerHeight);
document.body.appendChild(renderer.domElement);

//纹理
const texture = new THREE.TextureLoader().load('./particles/1.png');

const params = {
    count: 200000,
    size: 0.01,
    radius: 5,
    branch: 5,
    color: 0xff6030,
    rotateScale: 0.2
};

const randomColor = [];
for (let i = 0; i < 20; i++) {
    const number = Math.random();
    randomColor.push(number);
}

const galaxy = function () {
    const bufferGeometry = new THREE.BufferGeometry();
    const vertices = new Float32Array(params.count * 3);
    const color = new Float32Array(params.count * 3);
    for (let i = 0; i < params.count; i++) {
        const branchAngel = (i % params.branch) * (Math.PI * 2 / params.branch);
        const distance = Math.random() * params.radius * Math.pow(Math.random(), 0.5);

        //3次函数图像*distance 建立沿着半径的不均匀变化扩散，
        const randomX = Math.pow(Math.random() * 2 - 1, 3) * distance / 2;
        const randomY = Math.pow(Math.random() * 2 - 1, 3) * distance / 2;
        const randomZ = Math.pow(Math.random() * 2 - 1, 3) * distance / 2;

        const number = i * 3;
        vertices[number] = Math.cos(branchAngel + distance * params.rotateScale) * distance + randomX;
        vertices[number + 1] = Math.sin(branchAngel + distance * params.rotateScale) * distance + randomY;
        vertices[number + 2] = randomZ;



        if (i % params.branch == 0) {
            color[number] = randomColor[1];
            color[number + 1] = randomColor[2]
            color[number + 2] = randomColor[3]
        } else if (i % params.branch == 1) {
            color[number] = randomColor[4];
            color[number + 1] = randomColor[5];
            color[number + 2] = randomColor[6];
        } else if (i % params.branch == 2) {
            color[number] = randomColor[7];
            color[number + 1] = randomColor[8];
            color[number + 2] = randomColor[9];
        } else if (i % params.branch == 3) {
            color[number] = randomColor[10];
            color[number + 1] = randomColor[11];
            color[number + 2] = randomColor[12];
        } else if (i % params.branch == 4) {
            color[number] = randomColor[13];
            color[number + 1] = randomColor[14];
            color[number + 2] = randomColor[15];
        }
    };
    bufferGeometry.setAttribute('position', new THREE.BufferAttribute(vertices, 3));
    bufferGeometry.setAttribute('color', new THREE.BufferAttribute(color, 3));

    const material = new THREE.PointsMaterial({
        //color: 0xff0000,
        size: 0.01,
        vertexColors: true,
        //map:texture,
        alphaMap: texture,
        transparent: true,
        depthWrite: false//透明贴图的透明对深度模型有影响嘛
    });
    const points = new THREE.Points(bufferGeometry, material);
    scene.add(points);
    return points;
}

const points = galaxy();



//控制
const orbitControls = new OrbitControls(camera, renderer.domElement);
orbitControls.enableDamping = true;

//坐标轴
const axesHelper = new THREE.AxesHelper(5);
scene.add(axesHelper);


//帧动画
const clock = new THREE.Clock();
function animate() {
    const time = clock.getElapsedTime();
    points.rotation.z = 5 * time;
    orbitControls.update();
    renderer.render(scene, camera);
    requestAnimationFrame(animate);
};
animate();

//窗口调整侦听
window.addEventListener('resize', () => {
    camera.aspect = window.innerWidth / window.innerHeight;
    camera.updateProjectionMatrix();
    renderer.setSize(window.innerWidth, window.innerHeight);
    renderer.setPixelRatio(window.devicePixelRatio);
});


